package profController;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BeanQuestions;
import model.Utilisateur;

/**
 * Servlet implementation class ServletHomeProf
 */
@WebServlet("/ServletHomeProf")
public class ServletHomeProf extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletHomeProf() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
//		System.out.println("TEST ENCODAGE reponse: "+response.getCharacterEncoding());
		response.setCharacterEncoding("UTF-8");
//		System.out.println("->TEST ENCODAGE response: "+response.getCharacterEncoding());
//		System.out.println("TEST ENCODAGE request: "+request.getCharacterEncoding());
		request.setCharacterEncoding("UTF-8");
//		System.out.println("->TEST ENCODAGE response: "+request.getCharacterEncoding());
		
		
		Utilisateur utilisateurCourant = (Utilisateur)session.getAttribute("Usersession");
		
		//si l'utilisateur est loger et que c'est un prof on lui donne accés à l'espace prof, sinon on le renvoi sur la page d'authentification
		if(utilisateurCourant != null && utilisateurCourant.isProf())
		{
			if(session.getAttribute("monBeanQuestions") == null)
			{
				BeanQuestions monBeanQuestions = new BeanQuestions();
				try {
					monBeanQuestions.createListeQuestion();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				session.setAttribute("monBeanQuestions", monBeanQuestions);
			}
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("prof/homeProf.jsp");			
			dispatcher.forward(request,response);	
		}
		else
		{
			String strDispatch = "/Test_Projet_QCM/Authentification";
			response.sendRedirect(strDispatch);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
